/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package extension.phom.handler;

import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.extensions.BaseClientRequestHandler;
import extension.room.analyse.Card;

import extension.room.phom.PhomExtension;
import extension.room.phom.PhomGame;
import extension.room.phom.PhomPlayer;
import extension.room.phom.PhomResponse;
import java.util.List;
import log.Debug;

/**
 *
 * @author daz
 */
public class TimeOutHandler extends BaseClientRequestHandler {

    @Override
    public void handleClientRequest(User user, ISFSObject params) {
        PhomExtension phomExtension = (PhomExtension) getParentExtension();
        PhomGame game = phomExtension.getGame();
        if (!game.getIsPlay()) {
            return;
        }
        PhomPlayer p = game.getPlayerFight();
        PhomResponse response = phomExtension.getResponse();
        if (p.getShowPhomOrder() >= 0) {
            final PhomPlayer pDelay = p;
            final User uDelay = user;
            final PhomGame gDelay = game;
            final PhomExtension extDelay = phomExtension;
            final PhomResponse responseDelay = response;
            Thread mainThread = new Thread(new Runnable() {

                @Override
                public void run() {
                    long startTime = System.currentTimeMillis();
                    while (System.currentTimeMillis() - startTime < 500) {
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException ex) {
                        }
                    }
                    if (pDelay.getName().equals(uDelay.getName())) {
                        int cardFight = pDelay.getCardCanFight();
                        gDelay.removeCardInHand(cardFight);
                        gDelay.setCardFight(cardFight);
                        if (pDelay.getShowPhomOrder() == -1) {
                            pDelay.generateListPhom();
                        }
                        List<User> listSend = extDelay.getParentRoom().getUserList();
                        listSend.remove(uDelay);
                        logAutomaticFight(uDelay.getName(), cardFight);
                        responseDelay.sendFightCard(cardFight, 1, gDelay.getGameTurn(), listSend);
                        responseDelay.sendFightCard(cardFight, 1, 1, gDelay.getGameTurn(), pDelay.getListPhom(), uDelay);
                        if (pDelay.getListCardIds().isEmpty()) {
                            gDelay.onFinishU(PhomGame.NOT_HAVE_PHOM);
                        }
                        gDelay.nextTurn();
                    }
                }
            });
            mainThread.start();
        } else {
            if (p.getName().equals(user.getName())) {
                int cardFight = p.getCardCanFight();
                game.removeCardInHand(cardFight);
                game.setCardFight(cardFight);
                if (p.getShowPhomOrder() == -1) {
                    p.generateListPhom();
                }
                List<User> listSend = phomExtension.getParentRoom().getUserList();
                listSend.remove(user);
                logAutomaticFight(user.getName(), cardFight);
                response.sendFightCard(cardFight, 1, game.getGameTurn(), listSend);
                response.sendFightCard(cardFight, 1, 1, game.getGameTurn(), p.getListPhom(), user);
                if (p.getListCardIds().isEmpty()) {
                    game.onFinishU(PhomGame.NOT_HAVE_PHOM);
                }
                game.nextTurn();
            }
        }
    }

    private void logAutomaticFight(String name, int cardFight) {
        Debug.d(name + "-auto fight:" + Card.getName(cardFight));
    }
}
